using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using TanashCMS.Business;

public partial class manager_dialogs_SelectImage : CMSManagementPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected string UploadButton1_UploadClick(object sender, WebControls.UploadButtonEventArgs e)
    {
        //return Path.GetFileName(e.FileUploadControl.PostedFile.FileName);
        /*do database storage of file here...*/
        string path = "";
        string destinationfolder = "";
        string filename = "";
        HttpPostedFile myFile = null;
        try
        {
            // make sure destination folder exists
            //if (Request.Form["currentFolderField"] != null)
                //destinationfolder = Request.Form["currentFolderField"];
            //else
                destinationfolder = "~/temp/";
            destinationfolder = mWebsite.ImagesFolder + uploadFolder.Value;

            path = Server.MapPath(destinationfolder);
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            DirectoryInfo dir = new DirectoryInfo(path);
            if (dir == null)
                return e.FileUploadControl.PostedFile.FileName;

            // get file info from file control
            if (e.FileUploadControl.PostedFile != null && e.FileUploadControl.PostedFile.ContentLength > 0)
            {
                filename = System.IO.Path.GetFileNameWithoutExtension(e.FileUploadControl.PostedFile.FileName);

                // clean the filename
                filename = Regex.Replace(filename, @"[^\w-_]", "");

                string extension = System.IO.Path.GetExtension(e.FileUploadControl.PostedFile.FileName);
                myFile = e.FileUploadControl.PostedFile;
                int nFileLen = myFile.ContentLength;
                byte[] myData = new byte[nFileLen];
                myFile.InputStream.Read(myData, 0, nFileLen);

                FileStream newFile = new FileStream(path + filename + extension, FileMode.Create);
                newFile.Write(myData, 0, myData.Length);
                newFile.Close();
                newFile.Dispose();

                // now load the image into a more usable object
/*                System.Drawing.Image fullSizeImg = null;
                try
                {
                    fullSizeImg = System.Drawing.Image.FromFile(path + filename + extension);
                    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                if (fullSizeImg == null)
                    return e.FileUploadControl.PostedFile.FileName;*/

                // rotate the image 360 degress to remove embedded thumbnail information, if it exists
                // this is done so that embedded thumbnail images (from digital cameras) are not used when resizing up to 400 x 300
                // instead the original image is used (because the rotation destroys the embedded thumbnail information)
/*                fullSizeImg.RotateFlip(RotateFlipType.Rotate180FlipNone);
                fullSizeImg.RotateFlip(RotateFlipType.Rotate180FlipNone);

                // resize it to 400 / 300
                ImageSizer size = new ImageSizer(path + filename + extension);
                Size thumbnailSize = size.getNewSize(400, 300);
                System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                System.Drawing.Image fullSizeCopy = fullSizeImg.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, dummyCallBack, IntPtr.Zero);
                fullSizeImg.Dispose();

                // save the resized version over the temporary file
                fullSizeCopy.Save(path + filename + "_.png", ImageFormat.Png);
                fullSizeCopy.Dispose();
                

                // load the new resized version as the original
                fullSizeImg = System.Drawing.Image.FromFile(path + filename + extension);

                // create a 100 / 75 thumbnail for lists
                size = new ImageSizer(path + filename + extension);
                thumbnailSize = size.getNewSize(100, 75);
                System.Drawing.Image thumbNail100 = fullSizeImg.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, dummyCallBack, IntPtr.Zero);
                thumbNail100.Save(path + filename + "_100" + ".png", ImageFormat.Png);

                // create a 200 / 150 thumbnail for small previews
                size = new ImageSizer(path + filename + extension);
                thumbnailSize = size.getNewSize(200, 150);
                System.Drawing.Image thumbNail200 = fullSizeImg.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, dummyCallBack, IntPtr.Zero);
                thumbNail200.Save(path + filename + "_200" + ".png", ImageFormat.Png);*/

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return filename;
    }

    //this function is reqd for thumbnail creation
    public bool ThumbnailCallback()
    {
        return false;
    }
}
